package Controller;

import Excption.moneyIsNotEnven;
import Service.BillService;
import Service.MouthlyBudgetService;
import Service.UserService;
import Utils.ThreadLocalUtis;
import pojo.Bill;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class StartController {
    private Scanner scanner = new Scanner(System.in);
    private UserService userService;
    private String username;
    private String password;
    private BillService billService;
    private MouthlyBudgetService  mouthlyBudgetService;
    public void login() throws SQLException {
        userService = new UserService();
        boolean loginflag;
        System.out.println("======================================");
        System.out.println("请先登录个人账户管理系统");
        do {
            System.out.println("请输入用户名");
            username = scanner.nextLine();
            System.out.println("请先输入密码");
            password = scanner.nextLine();
            loginflag = userService.login(username, password);
            if (!loginflag) {
                System.out.println("密码错误，请重新输入");
            }
        } while (!loginflag);
        Map<String, Object> map = new HashMap<>();
        int idByusername = userService.getUserIdByusername(username);
        map.put("username", username);
        map.put("userid", idByusername);
        ThreadLocalUtis.set(map);
        System.out.println("恭喜你登陆成功， 欢迎来到" + username + "的个人账户系统");
    }

    public void Start() throws Exception {
        login();
        Scanner scanner = new Scanner(System.in);
        billService = new BillService();
        int choose;
        loop:
        do {
            StartPrint();
            choose = scanner.nextInt();
            looploop:
            switch (choose) {
                case 1: {
                    System.out.println("请输入收入信息：");
                    System.out.println("日期（YYYY-MM-DD）：");
                    String localdate = scanner.next();
                    System.out.println("金额：");
                    double money = scanner.nextDouble();
                    System.out.println("请输入账单的名字");
                    String billName = scanner.next();
                    System.out.println("类别:（如工资， 奖金等）");
                    String type = scanner.next();
                    System.out.println("备注");
                    String comment = scanner.next();
                    boolean b = billService.inputBill(localdate, money, billName, type, comment);
                    if (b) {
                        System.out.println("收入已经成功记录");
                        break;
                    }
                    System.out.println("收入记录失败，请重新尝试");
                    break;
                }
                case 2: {
                    System.out.println("进入了记录支出功能");
                    System.out.println("请输入支出信息：");
                    System.out.println("日期（YYYY-MM-DD）：");
                    String localdate = scanner.next();
                    System.out.println("金额：");
                    double money = scanner.nextDouble();
                    System.out.println("请输入账单的名字");
                    String billName = scanner.next();
                    System.out.println("类别:（如工资， 奖金等）");
                    String type = scanner.next();
                    System.out.println("备注");
                    String comment = scanner.next();
                    boolean b = false;
                    try {
                        b = billService.outputBill(localdate, money, billName, type, comment);
                        if (b) {
                            System.out.println("记录支出功能成功");
                            break;
                        }
                        System.out.println("记录支出功能失败");
                        break;
                    } catch (moneyIsNotEnven e) {
                        System.out.println("记录支出功能失败");
                        break;
                    }
                }
                case 3: {
                    List<Bill> outputBills = billService.getOutputBills();
                    if (outputBills == null) {
                        System.out.println("查询为空");
                        break;
                    }
                    if (outputBills.size() == 0) {
                        System.out.println("当前的账单的支出的记录为空， 请添加后再来");
                        break;
                    }
                    System.out.print("账单支出的是:");
                    for (int i = 0; i < outputBills.size(); i++) {
                        System.out.println(outputBills.get(i));
                    }
                    System.out.println("=====================================================");
                    System.out.println("======================账单存入==========================");
                    List<Bill> inputBill = billService.getInputBill();
                    if (inputBill == null) {
                        System.out.println("服务器发生了错误");
                    }
                    for (int i = 0; i < inputBill.size(); i++) {
                        System.out.println(inputBill.get(i));
                    }
                    Thread.sleep(1000);
                    break;
                }
                case 4: {
                    System.out.println("请选择你要根据什么方式查询账单");
                    System.out.println("1 : 按照指定日期查询 " +
                            "2： 按照日期的范围查询" +
                            "3:按照类别查询账单" + "" +
                            "4: 重新选择查询方式");

                    int choss = scanner.nextInt();
                    switch (choss) {
                        case 1: {
                            System.out.println("现在查询的模式是指定对应的日期查询：");
                            String date = scanner.next();
                            List<Bill> inputBillForTime = billService.getInputBillForTime(date);
                            if (inputBillForTime == null) {
                                System.out.println("查询的账单为空");
                                break;
                            }
                            System.out.println("============存入的账单====================");
                            for (int i = 0; i < inputBillForTime.size(); i++) {
                                System.out.println(inputBillForTime.get(i));
                            }
                            System.out.println("===============支出的账单=====================");
                            List<Bill> outputBills = billService.getOutputBills(date);
                            if (outputBills == null) {
                                System.out.println("查询的支出的账单为空");
                                break;
                            }
                            for (int i = 0; i < outputBills.size(); i++) {
                                System.out.println(outputBills.get(i));
                            }
                            break;
                        }
                        case 2: {

                        }
                        case  3 :{

                        }
                        case  4 : {
                            break ;
                        }
                    }
                }
                case 5 : {
                    System.out.println("设置月度预算");
                    mouthlyBudgetService  =new MouthlyBudgetService();
                    boolean b = mouthlyBudgetService.setMouthlyBudget();
                    if (b){
                        System.out.println("修改成功");
                        break;
                    }
                    System.out.println("修改失败");
                    break ;
                }
                case 6 : {
                    System.out.println("查询月度账单");
                    System.out.println("查询当前月份的账单 1 ， 查询指定的月份的账单");
                    int  intchoose  =  scanner.nextInt();
                    switch (intchoose) {
                        case  1 :{
                            List<List<Bill>> monthBill = billService.getMonthBill();
                            System.out.println("当前月份收入的账单的： ");
                            List<Bill> bill1 = monthBill.get(0);
                            for (int i = 0; i < bill1.size(); i++) {
                                System.out.println(bill1.get(i));
                            }
                            System.out.println("当前月份支出的账单的是:");
                            List<Bill> bill2 =monthBill.get(1);
                            for (int i = 0; i < bill2.size(); i++) {
                                System.out.println(bill2.get(i));
                            }
                            break ;
                        }
                        case 2 :{
                            System.out.println("请指定对应的年份");
                            int year =  scanner.nextInt();
                            System.out.println("请指定当前的"+year+"中的月份");
                            int month = scanner.nextInt();
                            List<List<Bill>> monthBill = billService.getMonthBill(year, month);
                            System.out.println("当前月份收入的账单的： ");
                            List<Bill> bill1 = monthBill.get(0);
                            for (int i = 0; i < bill1.size(); i++) {
                                System.out.println(bill1.get(i));
                            }
                            System.out.println("当前月份支出的账单的是:");
                            List<Bill> bill2 =monthBill.get(1);
                            for (int i = 0; i < bill2.size(); i++) {
                                System.out.println(bill2.get(i));
                            }
                            break ;
                        }
                    }
                }
            }
        } while (choose != 7);
        System.out.println("正在退出系统......");
        Thread.sleep(2000);
        System.out.println("系统退出成功!");

    }

    private void StartPrint() {
        System.out.println("=================================\n" +
                "欢迎使用个人账单管理系统\n" +
                "=================================\n" +
                "请选择操作：\n" +
                "1. 记录收入\n" +
                "2. 记录支出\n" +
                "3. 查看所有账单（第三周任务添加）\n" +
                "4. 查询账单（第三周任务添加）\n" +
                "5. 设置月度预算（第五周任务添加）\n" +
                "6. 查看月度统计报告（第五周任务添加）\n" +
                "7. 退出系统");
    }

    private void loginPrint() {
        System.out.println("======================================");
        System.out.println("请先登录个人账户管理系统");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        username = scanner.nextLine();
        System.out.println("请先输入密码");
        password = scanner.nextLine();
    }

    private void inputBill() {
        System.out.println("请输入收入信息：");

        System.out.println("日期（YYYY-MM-DD）：");
        System.out.println("金额：");
        System.out.println("类别:（如工资， 奖金等）");
        System.out.println("备注");
        System.out.println("收入已经成功记录");

    }

}
